using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
#if SILVERLIGHT
using System.Windows;
#endif

namespace SBPweb.Common.Diagnostics
{
	/// <summary>
	/// Class of FileInfo
	/// </summary>
	public class FileInfo
	{

		#region Private fields

		private System.Version mFileVersion;
		private System.Version mAsmVersion;
		private System.Version mProductVersion;
		private string mPackageName;
		private string mDescription;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of FileInfo
		/// </summary>
		public FileInfo()
		{
		}

		/// <summary>
		/// Creates new FileInfo
		/// </summary>
		/// <param name="path">path of file</param>
		/// <returns>FileInfo item</returns>
		public static FileInfo CreateFileInfo(string path)
		{
			FileInfo fI = new FileInfo();

#if !SILVERLIGHT
			FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path);

			fI.FileVersion = new System.Version(fvi.FileVersion);
			fI.ProductVersion = new System.Version(fvi.ProductVersion);
			fI.PackageName = fvi.ProductName;
			fI.Description = fvi.FileDescription;

            AssemblyName an = AssemblyName.GetAssemblyName(path);
#else
            System.IO.FileInfo sfi = new System.IO.FileInfo(path);
            throw new NotImplementedException();
            AssemblyName an = new AssemblyName();
            an.CodeBase = path;
#endif
			fI.AssemblyVersion = an.Version;

			return fI;
		}

		#endregion

		#region Public property accessors

		/// <summary>
		/// File version
		/// </summary>
		public System.Version FileVersion
		{
			get
			{
				return mFileVersion;
			}
			protected set
			{
				mFileVersion = value;
			}
		}

		/// <summary>
		/// Assembly version
		/// </summary>
		public System.Version AssemblyVersion
		{
			get
			{
				return mAsmVersion;
			}
			protected set
			{
				mAsmVersion = value;
			}
		}

		/// <summary>
		/// Product version
		/// </summary>
		public System.Version ProductVersion
		{
			get
			{
				return mProductVersion;
			}
			protected set
			{
				mProductVersion = value;
			}
		}

		/// <summary>
		/// Package name
		/// </summary>
		public string PackageName
		{
			get
			{
				return mPackageName;
			}
			protected set
			{
				mPackageName = value;
			}
		}

		/// <summary>
		/// Description
		/// </summary>
		public string Description
		{
			get
			{
				return mDescription;
			}
			protected set
			{
				mDescription = value;
			}
		}

		#endregion

		#region Public helper methods

		/// <summary>
		/// Stores a temporary file
		/// </summary>
		/// <param name="fileName">new filename</param>
		/// <param name="bytes">content of file</param>
		/// <returns>path of new file</returns>
		public static string StoreFileTemporary(string fileName, byte[] bytes)
		{
#if !SILVERLIGHT
            string path = AppDomain.CurrentDomain.DynamicDirectory + "\\" + fileName;
#else
            string path = Application.Current.Host.Source.LocalPath + "\\" + fileName;
#endif

			FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
			BinaryWriter bw = new BinaryWriter(fs);

			bw.Write(bytes);
			bw.Flush();
			bw.Close();

			fs.Close();

			return path;
		}

		/// <summary>
		/// Stores a temporary file
		/// </summary>
		/// <param name="fileName">new filename</param>
		/// <param name="stream">stream of file</param>
		/// <returns>path of new file</returns>
		public static string StoreFileTemporary(string fileName, Stream stream)
		{
#if !SILVERLIGHT
            string path = AppDomain.CurrentDomain.DynamicDirectory + "\\" + fileName;
#else
            string path = Application.Current.Host.Source.LocalPath + "\\" + fileName;
#endif

			FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
			BinaryWriter bw = new BinaryWriter(fs);
			StreamReader sr = new StreamReader(stream);

			bw.Write(sr.ReadToEnd());
			bw.Flush();
			bw.Close();

			fs.Close();

			return path;
		}

		#endregion

	}
}
